package net.ib.mtalk.ui.attachment.vcalendar;

import java.util.ArrayList;
import java.util.List;

import net.ib.mtalk.ui.attachment.vcalendar.VCalendarStruct.EventStruct;

public class VCalendarMnParser {
	
	private VCalendarStruct vCalendarStructData;
	private String vcalendarData;
	private EventStruct vCalendarEvent;
	
	public VCalendarMnParser(){
		vCalendarStructData = new VCalendarStruct();
		vCalendarEvent = new EventStruct();
	}
	
	public VCalendarStruct parse(String vcalendarStr){
		
		List<String> lineData = new ArrayList<String>();
		String dataTemp;
		vcalendarData = vcalendarStr;
		int start = 0, end = 0;
		char ch = 0;
		
		for(int i = 0; i < vcalendarData.length(); i++){
			ch = vcalendarData.charAt(i);
			if(ch == '\n'){
				end = i;
				lineData.add(vcalendarData.substring(start, end));
				start = i + 1;
			}
		}
		
		for(int i = 0; i < lineData.size(); i++){
			dataTemp = lineData.get(i);
			for(int j = 0; j < dataTemp.length(); j++){
				ch = dataTemp.charAt(j);
				if(ch == ':'){
					if (dataTemp.substring(0, j).equals("END")) {
						break;
					} else if(dataTemp.substring(0, j).equals("SUMMARY")) {
						vCalendarEvent.title = dataTemp.substring(j + 1,
								dataTemp.length());
						break;
					} else if(dataTemp.substring(0, j).equals("DTSTART")) {
						vCalendarEvent.dtstart = dataTemp.substring(j + 1,
								dataTemp.length());
						break;
					} else if(dataTemp.substring(0, j).equals("DTEND")) {
						vCalendarEvent.dtend = dataTemp.substring(j + 1,
								dataTemp.length());
						break;
					} else if(dataTemp.substring(0, j).equals("DUE")) {
						vCalendarEvent.duration = dataTemp.substring(j + 1,
								dataTemp.length());
						break;
					} else if(dataTemp.substring(0, j).equals("DESCRIPTION")) {
						vCalendarEvent.description = dataTemp.substring(j + 1,
								dataTemp.length());
						break;
					} else if(dataTemp.substring(0, j).equals("LOCATION")) {
						vCalendarEvent.event_location = dataTemp.substring(j + 1,
								dataTemp.length());
						break;
					} else if(dataTemp.substring(0, j).equals("COMPLETED")) {
						vCalendarEvent.last_date = dataTemp.substring(j + 1,
								dataTemp.length());
						break;
					} else if(dataTemp.substring(0, j).equals("RRULE")) {						
						vCalendarEvent.rrule = dataTemp.substring(j + 1,
								dataTemp.length());
						break;
					} else if(dataTemp.substring(0, j).equals("STATUS")) {
						vCalendarEvent.status = dataTemp.substring(j + 1,
								dataTemp.length());
						break;
//					} else if(dataTemp.substring(0, j).equals("ALLDAY")) {
//						vCalendarEvent.status = dataTemp.substring(j + 1,
//								dataTemp.length());
//						break;
//					} else if(dataTemp.substring(0, j).equals("ALARM")) {
//						vCalendarEvent.status = dataTemp.substring(j + 1,
//								dataTemp.length());
//						break;
//					} else if(dataTemp.substring(0, j).equals("TZ")) {
//						vCalendarEvent.status = dataTemp.substring(j + 1,
//								dataTemp.length());
//						break;
					} else {
						break;
					}
				}
			}
		}
		vCalendarStructData.addEventList(vCalendarEvent);
		return vCalendarStructData;
	}
	
	public String parseName(String vcalendarStr) {

		List<String> lineData = new ArrayList<String>();
		String dataTemp, dataName = null, startStr, endStr;
		vcalendarData = vcalendarStr;
		int start = 0;
		int end = 0;
		char ch = 0;

		// '\n'(���� ��) �϶��� �����Ͽ� �ٴ����� ����� ����
		for (int i = 0; i < vcalendarData.length(); i++) {
			ch = vcalendarData.charAt(i);
			if (ch == '\n') {
				end = i;
				lineData.add(vcalendarData.substring(start, end));
				start = i + 1;
			}
		}
		for (int i = 0; i < lineData.size(); i++) {
			dataTemp = lineData.get(i);
			for (int j = 0; j < dataTemp.length(); j++) {
				ch = dataTemp.charAt(j);
				if (ch == ':') {
					if (dataTemp.substring(0, j).equals("END")) {
						break;
					} else if(dataTemp.substring(0, j).equals("SUMMARY")) {
						dataName = dataTemp.substring(j + 1,
								dataTemp.length());
						break;
					}
				}
			}
		}
		return dataName;
	}
}
